import axios from 'axios';
import { BASE_URL } from './tools'

const instance=axios.create({
    baseURL:BASE_URL,
    timeout:5000,
});

// 请求拦截 (可以把统一的请求头儿放里面)
instance.interceptors.request.use(
    function(config) {
      return config;
    },
    function(error) {
      return Promise.reject(error);
    }
  );
  
  // 响应拦截
  instance.interceptors.response.use(
    function(response) {
      return response.data;
    },
    function(error) {
      return Promise.reject(error);
    }
  );
  // 封装get请求
  export const get = (url, params) => instance.get(url, { params: params });
  // 封装post请求
  export const post = (url, data) => instance.post(url, data);
  
  export default instance;
  
  /*
  axios.get('/user', {
  params: {
      ID: 12345
  }
  })
  axios.post('/user', {
      firstName: 'Fred',
      lastName: 'Flintstone'
    })
  */
  